<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@ page import ="com.easyattendance.model.Teacher" %>
<%@ page import ="com.easyattendance.services.AddClassService" %>
<%@ page import ="com.easyattendance.model.Class" %>
<%@ page import ="com.easyattendance.services.AddSubjectService" %>
<%@ page import ="com.easyattendance.model.Subject" %>
<%@ page import ="com.easyattendance.model.SubjectClass" %>
<html>
<head>
<title>Teacher - Add Assignment </title>
<!-- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> -->
<jsp:include page="../includeFiles.jsp" />

<link rel="stylesheet" href="<%=request.getContextPath()%>/views/css/ui/jquery-ui-1.10.4.custom.min.css">
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.core.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.widget.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.datepicker.js"></script>
<script>
	$(function() {
		$( "#assignment_date" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			dateFormat: 'yy-mm-dd',
			beforeShowDay: noSunday,
			onClose: function( selectedDate ) {
				$( "#assignment_sub_date" ).datepicker( "option", "minDate", selectedDate );
			}
		});
		$( "#assignment_sub_date" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			dateFormat: 'yy-mm-dd',
			beforeShowDay: noSunday,
			onClose: function( selectedDate ) {
				$( "#assignment_date" ).datepicker( "option", "maxDate", selectedDate );
			}
		});
	});
	function noSunday(date){ 
	     return [date.getDay() != 0, ''];
	};
</script>
</head>

<%
	String currentUser=(String)session.getAttribute("currentUser");
	AddTeacherService teacherService;	
	String teacherId=null;
		if(currentUser!=null)
		{
			teacherService=new AddTeacherService();
			teacherId=teacherService.getTeacherByEmailId(currentUser).getTeacher_id();
		}	

%>
<body class="left-sidebar" onload="getValues()">

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp" />
<jsp:include page="../common/checkTeacherRole.jsp" />
	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">

				<!-- Login -->
						<div id="login_content">
						<header>
							<h2>Add Assignment</h2>
						</header>
						
						<span class="error_msg">${assignmentExists}</span>
						<span class="success_msg">${assignmentAdded}</span>
						<span class="error_msg">${assignmentNotAdded}</span>
						<span class="error_msg">${file_upload_error}</span>
						<span class="error_msg">${null_values}</span>
						<span class="error_msg">${date_null_values}</span>
						<div class="submit_errors_msg">
							<ul>
								<li id="submit_is_field_empty" class="submit_errors">All * fields are required.</li>
							</ul>
						</div>
						<form name="addAssignmentForm" action="${pageContext.request.contextPath}/AddAssignmentServlet" method="post" enctype="multipart/form-data" onsubmit="return checkFormErrors();">
							<!-- Hidden field -->
							<input type="hidden" id="action_type" name="action_type" value="create">
							
							<!-- Assignment of which class -->
							<div>
							    <label>Class: <span class="error_msg">*</span></label>
								<select name="select_class" id="select_class" tabindex="1" value=<script>getCookie("select_class")</script>>
									<option value="">Select Class
										<% AddClassService addClassService = new AddClassService();	
									List<SubjectClass> classList = new ArrayList<SubjectClass>();
									classList = addClassService.listClassesByTeacherId(teacherId);
									SubjectClass tempClass = null;
									Iterator classItr = classList.iterator();
								
									while(classItr.hasNext()) { %>
										<% tempClass = (SubjectClass) classItr.next();
											String classId = tempClass.getClass_id();%>
										<option value='<%=classId %>'><%= classId %>
									<% } %>
								</select>
							</div>
							
							<%
								String classId=request.getParameter("class");
							%>
							
							<input type="hidden" id ="class_id" name="class_id"  value="<%=classId %>">
							
							<!-- Subjects depend upon class selected -->
							<div>
								<label>Subject: <span class="error_msg">*</span></label>
								<select name="subject_name" id="subject_name" tabindex="2" >
									<option value="">Select Subject
									<% AddSubjectService addSubjectService = new AddSubjectService();	
									List<Subject> subjectList = new ArrayList<Subject>();
									subjectList = addSubjectService.listSubjectsByTeacherIdForClass(teacherId,classId);
									//subjectList = addSubjectService.listSubjects();
									String tempSubject = null;
									Iterator subjectItr = subjectList.iterator();
								  
									while(subjectItr.hasNext()) { %>
										<% tempSubject =(String) subjectItr.next();
										String subjectName = addSubjectService.getSubjectBySubjectId(tempSubject).getSubject_name(); %>
										<option value='<%=subjectName %>'><%= subjectName %>
									<% } %>
								</select>
							</div>
							
							<!-- Assignment Number-->
							<div>
								<label> Assignment Number</label>
								<input type="text" id="assignment_id" name="assignment_id" tabindex="3" placeholder="Assignment Number">
							</div>
							
							<!-- Name of the assignment -->
							<div>
								<label> Assignment Name: <span class="error_msg">*</span></label>
								<input type="text" id="assignment_name" name="assignment_name" tabindex="4" placeholder="Assignment Name">
							</div>
							
							
							<!-- assignment given date -->
							<div>
								<label> Assignment Given Date: <span class="error_msg">*</span></label>
								<input type="text" id="assignment_date" name="assignment_date" tabindex="5" placeholder="Assignment given date">
							</div>
							
							<!-- assignment submission date -->
							<div>
								<label> Assignment Submission Date: <span class="error_msg">*</span></label>
								<input type="text" id="assignment_sub_date" name="assignment_sub_date" tabindex="6" placeholder="Submission Date">
							</div>
							
							<div> 
								<label> File Location: <span class="error_msg">*</span></label>
							</div>
							<div>
								<input id="file_name" name="file_name" type="text" placeholder="File Location" tabindex="7" style="width: 70%;float: left;margin-right: 2%;">
								<input type="file" value="Browse" name="fileName" class="button browse_button" tabindex="8">
							</div>
							
							<!--  hidden fields -->
								<input type="hidden" name="teacher_id" id="teacher_id" value="<%= teacherId %>">
								<input type="hidden" id="user_type" name="user_type" value="teacher">
							
							<div>	
								<input type="submit" value="Upload File" class="button" tabindex="9">
								<!-- <input type="button" value="Cancel" class="button"> -->
							</div>
						</form>
					</div>
			
			</div>
		</div>

		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<jsp:include page="../common/sidebarHeader.jsp" />

			<jsp:include page="teacherMenu.jsp" />

			<!-- Sidebar Footer Content -->
			<jsp:include page="../common/sidebarFooter.jsp" />

		</div>

	</div>	
	<script type="text/javascript">
		$(function(){
			$("#assignmentSettings").addClass("current_page_item");
		});
		var contextPath='<%=request.getContextPath()%>';
	</script>
	
	<script language="javascript">     

	$(".submit_errors_msg").hide();
	$(".submit_errors").hide();
	
	function checkFormErrors(){
		if(isAnyFieldEmpty($('#select_class'),$('#subject_name'),$('#assignment_name'),$('#assignment_date'),$('#assignment_sub_date'),$('#file_name'))){
			return true;
		}
		return false;
	}
	
     $(document).ready(function () {       

          $('input[name="fileName"]').change(function () {           

          var selectedValue = $(this).val();              

         $('input[name="file_name"]').val(selectedValue);          

         });           

     });
</script>

<!--  For drop down values -->
</script>
	<script type="text/javascript">
	
		$(function () {
		    var temp = 0;
		    $('#select_class').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#select_class").val();
				setCookie("select_class",select_class);
				window.location.replace(contextPath + "/views/teacher/addAssignment.jsp?class=" + select_class);
		    });      
		});
		
		
		$(function () {
		    var temp = 0;
		    $('#select_subject').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#select_class").val();
				var select_subject = $("#select_subject").val();
				setCookie("select_subject",select_subject);
				window.location.replace(contextPath + "/views/teacher/addAssignment.jsp?" + "class=" + select_class + "&subject=" + select_subject);
		    });      
		});
		
				
		function getValues(){
			$("#select_class").val(getCookie("select_class"));
			$("#select_subject").val(getCookie("select_subject"));
		}
		
		function getCookie(name) { 
			var re = new RegExp(name + "=([^;]+)"); 
			var value = re.exec(document.cookie);
			return (value != null) ? unescape(value[1]) : null; 
		}
		
		function setCookie(name, value) {
			  var today = new Date(); 
			  var expiry = new Date(today.getTime() + 1 * 24 * 3600 * 1000); // plus 30 days
			  document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
		 }
		
		function clearCookie(){
			deleteCookie("select_class");
			deleteCookie("select_subject");
			alert("Cookies Deleted");
		}
		
		function deleteCookie(name)
		{
			var expired = new Date(today.getTime() - 24 * 3600 * 1000); // less 24 hours
			document.cookie=name + "=null; path=/; expires=" + expired.toGMTString();
		}		
	</script>
		
</body>
</html>